;! function( Slider ) {
    Slider.prototype.initAnimation = function() {
        var subSwiper,
            initSubSwiper = function() {
                if ( ! subSwiper ) {
                    var $subPower = $( ".js-sub-power-scroll" );
                    subSwiper  = $subPower.swiper( {
                        mode: "horizontal",
                        slidesPerView:1,
                        loop: false
                    } );
                } else {
                    subSwiper.reInit();
                }
            },
            startSlideAnimation = function( swiper ) {
                var $activeSlide = $( swiper.activeSlide() );
                $activeSlide.children().removeClass( "hide" );
                // init sub swiper
                if ( $activeSlide.data( "containSubSwiper" ) || ( $activeSlide.find( ".js-sub-power-scroll" ).length > 0 ) ) {
                    $activeSlide.data( "containSubSwiper", true );
                    initSubSwiper();
                }
            },
            hideAllSlideAnimation = function( swiper ) {
                for ( var i = 0, l = swiper.slides.length; i < l; ++ i ) {
                    $( swiper.slides[i] ).children().addClass( "hide" );
                }
            };

        this.swiper.addCallback( "FirstInit", function( swiper ) {
            setTimeout( function() {
                startSlideAnimation( swiper );
            }, 300);
        } );

        this.swiper.addCallback( "SlideReset", function( swiper, direction ) {
            startSlideAnimation( swiper );
        } );

        this.swiper.addCallback( "SlideChangeEnd", function( swiper, direction ) {
            hideAllSlideAnimation( swiper );
            startSlideAnimation( swiper );
        } );

        hideAllSlideAnimation( this.swiper );
    };

    Slider.prototype.initInteraction = function() {
        var $power = this.$power,
            swiper = this.swiper;

        // new tab at pc
        if ( ! /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test( navigator.userAgent.toLowerCase() ) ) {
            $power.find( "a" ).attr( "target", "_blank" );
        }

        // rate btn
        $power.delegate( ".js-slide-rating .js-rate-btn", "click", function( e ) {
            var $btn    = $( ".js-slide-rating .js-rate-btn" ),
                $number = $( ".js-slide-rating .js-number" ),
                $hand  = $btn.find( ".js-hand" ),
                weiUid  = $btn.data( "wei-uid" );

            // 检测是否登陆微信了
            if ( ! WEIXIN_UID ) {
                if ( $btn.attr( "disabled" ) ) {
                    return false;
                }
                $btn.attr( "disabled", true );
                
                $.getJSON( API_HOME_PAGE + '/weixin/loginURL', { callback : encodeURIComponent( location.href.split( "#" )[0] ) }, function( res ) {
                    if ( ! res.error_code ) {
                        location.href = res.data.url;
                    } else {
                    }
                    $btn.attr( "disabled", false );
                } );
                return ;
            }

            // fixed the bug: after click rate btn, share link will contain the hash
            // fix method: set all hash to __fixed__
            window.location.hash = "__fixed__";
            $( ".swiper-slide" ).attr( "data-hash", "__fixed__" );

            if ( $btn.attr( "disabled" ) || ! weiUid ) {
                alert('亲今天已经点过靠谱了呀，明日再来哟');
                return false;
            }
            $btn.attr( "disabled", true );

            $number.html( parseInt( $number.html() || 0 ) + 1 ).addClass( "a-largen-out" );
            setTimeout( function(){ $number.removeClass( "a-largen-out" ); }, 780 );
            $hand.addClass( "rated" );
            $.getJSON( WEIXIN_HOME_PAGE + "/WeiUser/rateWeiUser", { id: weiUid }, function( res ) {
                if ( res && res.status ) {
                } else {
                    $number.html( parseInt( $number.html() || 0 ) - 1 ).removeClass( "a-largen-out" );
                    $hand.removeClass( "rated" );
                    $btn.attr( "disabled", false );
                    alert( res.info || "操作失败，请稍后重试" );
                }
            } );
        } );
    };
}( MobileSlider );
